import pygetwindow as gw
from PIL import ImageGrab

window_title = "操作文档.docx"  # 替换为你要截图的窗口标题
window = gw.getWindowsWithTitle(window_title)[0]

left, top, right, bottom = window.left, window.top, window.right, window.bottom

# 截图指定窗口
screenshot = ImageGrab.grab(bbox=(left, top, right, bottom))

# 保存截图
screenshot.save("screenshot.png")

# 显示截图（可选）
screenshot.show()

# window_titles = gw.getAllTitles()
# print(window_titles)